{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Imports\n",
    "\n",
    "Import all the modules and functionalities we need."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "\n",
    "# Import standard libraries.\n",
    "import os\n",
    "\n",
    "# Import third party libraries.\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "from statsmodels.stats.multitest import multipletests\n",
    "\n",
    "# Import custom libraries/scripts.\n",
    "import annotations\n",
    "import helpers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Loading data\n",
    "\n",
    "Fetch all our relevant data for the current analysis."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set working contants.\n",
    "EXPERIMENTS_PATH = r'E:\\Miguel\\PhD\\Results\\Competition\\DL\\Four-Arena Setup\\WT_mating_status\\processed'\n",
    "FPS = 60\n",
    "N_MINUTES = 60\n",
    "N_FRAMES = N_MINUTES * 60 * FPS\n",
    "INK = 'black'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set figure configurations.\n",
    "sns.set(\n",
    "        context='paper',\n",
    "        style='ticks',\n",
    "        font='sans-serif',\n",
    "        font_scale=1.0, \n",
    "        rc={\n",
    "            'axes.axisbelow': True,\n",
    "            'axes.edgecolor': INK,\n",
    "            'axes.facecolor': 'white' if INK=='black' else 'black',\n",
    "            'axes.grid': False,\n",
    "            'axes.labelcolor': INK,\n",
    "            'axes.labelsize': 13.0,\n",
    "            'axes.labelweight': 'normal',\n",
    "            'axes.linewidth': 1.0,\n",
    "            'axes.spines.left': True,\n",
    "            'axes.spines.bottom': True,\n",
    "            'axes.spines.top': False,\n",
    "            'axes.spines.right': False,\n",
    "            'axes.titlepad': 15.0,\n",
    "            'axes.titlesize': 20.0,\n",
    "            'axes.titleweight': 'bold',\n",
    "            'figure.facecolor': 'white' if INK=='black' else 'black',\n",
    "            'figure.figsize': [4.0, 4.0],\n",
    "            'figure.titlesize': 30.0,\n",
    "            'figure.titleweight': 'bold',\n",
    "            'font.family': ['sans-serif'],\n",
    "            'font.sans-serif': ['Arial'],\n",
    "            'legend.frameon': False,\n",
    "            'legend.fontsize': 11.0,\n",
    "            'lines.color': INK,\n",
    "            'lines.linewidth': 1.0,\n",
    "            'patch.edgecolor': INK,\n",
    "            'savefig.dpi': 300,\n",
    "            'savefig.format': 'png',\n",
    "            'savefig.bbox': 'tight',\n",
    "            'savefig.transparent': True,\n",
    "            'text.color': INK,\n",
    "            'text.usetex': False,\n",
    "            'xtick.color': INK,\n",
    "            'xtick.direction': 'out',\n",
    "            'xtick.labelsize': 12.0,\n",
    "            'xtick.major.pad': 5.0,\n",
    "            'xtick.major.size': 0.0,\n",
    "            'xtick.major.width': 1.0,\n",
    "            'xtick.minor.size': 0.0,\n",
    "            'xtick.minor.width': 0.4,\n",
    "            'ytick.color': INK,\n",
    "            'ytick.direction': 'out',\n",
    "            'ytick.labelsize': 12.0,\n",
    "            'ytick.major.pad': 5.0,\n",
    "            'ytick.major.size': 3.0,\n",
    "            'ytick.major.width': 1.0,\n",
    "            'ytick.minor.size': 0.0,\n",
    "            'ytick.minor.width': 0.4\n",
    "           }\n",
    "       )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Folder already exists, skipping.\n"
     ]
    }
   ],
   "source": [
    "# Prepare the Figures folder to save our graphs in.\n",
    "savepath = os.path.join(r'C:\\Users\\Miguel\\Desktop\\paper_data', 'paper_figures', 'figureS1')\n",
    "try:\n",
    "    os.makedirs(savepath)\n",
    "    print('New folder created.')\n",
    "except FileExistsError:\n",
    "    print('Folder already exists, skipping.')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Paths to conditions:\n",
      " ['E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin']\n"
     ]
    }
   ],
   "source": [
    "# Set the conditions to analyze.\n",
    "condition_order = ['virgin_virgin']\n",
    "conditions = [item.path for item in os.scandir(EXPERIMENTS_PATH) if item.name in condition_order]\n",
    "print('Paths to conditions:\\n', conditions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['video_2017-09-05T15_42_04_arena3', 'video_2017-09-07T13_28_02_arena4']\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "# Create a list of all the experiments that we DO NOT want to analyze.\n",
    "intruders = []\n",
    "for condition in conditions:\n",
    "    for file in os.listdir(condition):\n",
    "        if 'quality_control' in file:\n",
    "\n",
    "            # Read the quality.csv file to check which experiments are usable for analysis.\n",
    "            quality_file = os.path.join(condition, file)\n",
    "            quality_df = pd.read_csv(quality_file, usecols=[0,1], index_col=0)\n",
    "            quality_df = quality_df[quality_df['is_usable'] == False].index.values\n",
    "            \n",
    "            for value in quality_df:\n",
    "                intruders.append(value)\n",
    "\n",
    "print(intruders)             \n",
    "print(len(intruders))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\t\n",
      " virgin_virgin\n",
      "Copulation too short: video_2017-09-06T13_42_13_arena3\n",
      "Copulation too short: video_2017-09-26T14_52_18_arena3\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'virgin_virgin': ['E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-09-05T13_28_41_arena4',\n",
       "  'E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-09-05T14_34_55_arena2',\n",
       "  'E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-09-06T13_42_13_arena1',\n",
       "  'E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-09-06T14_45_32_arena3',\n",
       "  'E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-09-07T14_37_38_arena3',\n",
       "  'E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-09-07T14_37_38_arena4',\n",
       "  'E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-09-08T13_26_13_arena3',\n",
       "  'E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-09-12T13_30_06_arena4',\n",
       "  'E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-09-12T14_36_55_arena3',\n",
       "  'E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-09-12T16_18_15_arena4',\n",
       "  'E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-09-13T14_34_18_arena2',\n",
       "  'E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-09-20T13_43_53_arena3',\n",
       "  'E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-09-20T14_48_55_arena2',\n",
       "  'E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-09-21T13_38_55_arena4',\n",
       "  'E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-09-21T15_57_23_arena2',\n",
       "  'E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-09-26T13_40_19_arena1',\n",
       "  'E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-09-28T13_36_06_arena4',\n",
       "  'E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-09-28T14_44_01_arena3',\n",
       "  'E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-09-28T15_55_49_arena3',\n",
       "  'E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-10-12T15_50_43_arena2',\n",
       "  'E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-10-13T16_06_33_arena2',\n",
       "  'E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-10-18T15_19_17_arena3',\n",
       "  'E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-10-19T13_50_56_arena2',\n",
       "  'E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-10-20T13_21_06_arena2',\n",
       "  'E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-10-20T15_44_09_arena2',\n",
       "  'E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-10-24T16_13_35_arena4',\n",
       "  'E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-10-25T14_55_12_arena1',\n",
       "  'E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-11-02T13_37_48_arena3',\n",
       "  'E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-11-02T14_48_28_arena4',\n",
       "  'E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-11-02T16_11_06_arena3',\n",
       "  'E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-11-03T14_54_45_arena1',\n",
       "  'E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-11-03T16_20_50_arena4',\n",
       "  'E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-12-20T12_58_51_arena4',\n",
       "  'E:\\\\Miguel\\\\PhD\\\\Results\\\\Competition\\\\DL\\\\Four-Arena Setup\\\\WT_mating_status\\\\processed\\\\virgin_virgin\\\\video_2017-12-20T14_01_01_arena3']}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Load all usable experiments for each condition.\n",
    "experiments = {condition: [] for condition in condition_order}\n",
    "\n",
    "for condition_path in conditions:\n",
    "    \n",
    "    condition = os.path.basename(condition_path)\n",
    "    print('\\t\\n', condition)\n",
    "    \n",
    "    for item in os.scandir(condition_path):\n",
    "        if item.is_dir() and item.name not in intruders:\n",
    "\n",
    "            annotation_video = annotations.read(item.path + '.csv')\n",
    "            \n",
    "            try:\n",
    "                copulation = annotation_video[0].events[0]\n",
    "\n",
    "                # Filter out videos where copulation is interrupted.\n",
    "                copulation_end = copulation.time_interval[1]\n",
    "                if copulation_end==N_FRAMES:\n",
    "                    print('Copulation interrupted:', item.name)\n",
    "                    continue\n",
    "\n",
    "                # Filter out videos where copulation lasts less than 8 minutes.\n",
    "                copulation_duration = copulation.duration\n",
    "                if copulation_duration <= 8 * 60 * FPS:\n",
    "                    print('Copulation too short:', item.name)\n",
    "                    continue\n",
    "            \n",
    "            except IndexError:\n",
    "                continue\n",
    "\n",
    "            experiments[condition].append(item.path)\n",
    "\n",
    "experiments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "virgin_virgin \n",
      "\n",
      "video_2017-09-05T13_28_41_arena4\n",
      "Number of aggressive bouts is: 1\n",
      "Number of clean detected \"encounters\" is: 30\n",
      "Number of detected aggressive bouts is: 1\n",
      "1.0 \n",
      "\n",
      "video_2017-09-05T14_34_55_arena2\n",
      "Number of aggressive bouts is: 10\n",
      "Number of clean detected \"encounters\" is: 26\n",
      "Number of detected aggressive bouts is: 10\n",
      "1.0 \n",
      "\n",
      "video_2017-09-06T13_42_13_arena1\n",
      "Number of aggressive bouts is: 6\n",
      "Number of clean detected \"encounters\" is: 31\n",
      "Number of detected aggressive bouts is: 6\n",
      "1.0 \n",
      "\n",
      "video_2017-09-06T14_45_32_arena3\n",
      "Number of aggressive bouts is: 13\n",
      "Number of clean detected \"encounters\" is: 30\n",
      "Number of detected aggressive bouts is: 12\n",
      "0.9230769230769231 \n",
      "\n",
      "video_2017-09-07T14_37_38_arena3\n",
      "Number of aggressive bouts is: 7\n",
      "Number of clean detected \"encounters\" is: 22\n",
      "Number of detected aggressive bouts is: 7\n",
      "1.0 \n",
      "\n",
      "video_2017-09-07T14_37_38_arena4\n",
      "Number of aggressive bouts is: 5\n",
      "Number of clean detected \"encounters\" is: 29\n",
      "Number of detected aggressive bouts is: 5\n",
      "1.0 \n",
      "\n",
      "video_2017-09-08T13_26_13_arena3\n",
      "Number of aggressive bouts is: 12\n",
      "Number of clean detected \"encounters\" is: 56\n",
      "Number of detected aggressive bouts is: 12\n",
      "1.0 \n",
      "\n",
      "video_2017-09-12T13_30_06_arena4\n",
      "Number of aggressive bouts is: 5\n",
      "Number of clean detected \"encounters\" is: 20\n",
      "Number of detected aggressive bouts is: 5\n",
      "1.0 \n",
      "\n",
      "video_2017-09-12T14_36_55_arena3\n",
      "No aggression for this experiment\n",
      "\n",
      "video_2017-09-12T16_18_15_arena4\n",
      "Number of aggressive bouts is: 3\n",
      "Number of clean detected \"encounters\" is: 24\n",
      "Number of detected aggressive bouts is: 3\n",
      "1.0 \n",
      "\n",
      "video_2017-09-13T14_34_18_arena2\n",
      "Number of aggressive bouts is: 4\n",
      "Number of clean detected \"encounters\" is: 48\n",
      "Number of detected aggressive bouts is: 4\n",
      "1.0 \n",
      "\n",
      "video_2017-09-20T13_43_53_arena3\n",
      "No aggression for this experiment\n",
      "\n",
      "video_2017-09-20T14_48_55_arena2\n",
      "Number of aggressive bouts is: 2\n",
      "Number of clean detected \"encounters\" is: 25\n",
      "Number of detected aggressive bouts is: 2\n",
      "1.0 \n",
      "\n",
      "video_2017-09-21T13_38_55_arena4\n",
      "Number of aggressive bouts is: 3\n",
      "Number of clean detected \"encounters\" is: 24\n",
      "Number of detected aggressive bouts is: 3\n",
      "1.0 \n",
      "\n",
      "video_2017-09-21T15_57_23_arena2\n",
      "Number of aggressive bouts is: 19\n",
      "Number of clean detected \"encounters\" is: 42\n",
      "Number of detected aggressive bouts is: 18\n",
      "0.9473684210526315 \n",
      "\n",
      "video_2017-09-26T13_40_19_arena1\n",
      "Number of aggressive bouts is: 3\n",
      "Number of clean detected \"encounters\" is: 32\n",
      "Number of detected aggressive bouts is: 3\n",
      "1.0 \n",
      "\n",
      "video_2017-09-28T13_36_06_arena4\n",
      "No aggression for this experiment\n",
      "\n",
      "video_2017-09-28T14_44_01_arena3\n",
      "Number of aggressive bouts is: 3\n",
      "Number of clean detected \"encounters\" is: 57\n",
      "Number of detected aggressive bouts is: 3\n",
      "1.0 \n",
      "\n",
      "video_2017-09-28T15_55_49_arena3\n",
      "No aggression for this experiment\n",
      "\n",
      "video_2017-10-12T15_50_43_arena2\n",
      "No aggression for this experiment\n",
      "\n",
      "video_2017-10-13T16_06_33_arena2\n",
      "No aggression for this experiment\n",
      "\n",
      "video_2017-10-18T15_19_17_arena3\n",
      "Number of aggressive bouts is: 3\n",
      "Number of clean detected \"encounters\" is: 40\n",
      "Number of detected aggressive bouts is: 3\n",
      "1.0 \n",
      "\n",
      "video_2017-10-19T13_50_56_arena2\n",
      "No aggression for this experiment\n",
      "\n",
      "video_2017-10-20T13_21_06_arena2\n",
      "Number of aggressive bouts is: 6\n",
      "Number of clean detected \"encounters\" is: 56\n",
      "Number of detected aggressive bouts is: 5\n",
      "0.8333333333333334 \n",
      "\n",
      "video_2017-10-20T15_44_09_arena2\n",
      "Number of aggressive bouts is: 1\n",
      "Number of clean detected \"encounters\" is: 47\n",
      "Number of detected aggressive bouts is: 1\n",
      "1.0 \n",
      "\n",
      "video_2017-10-24T16_13_35_arena4\n",
      "Number of aggressive bouts is: 2\n",
      "Number of clean detected \"encounters\" is: 28\n",
      "Number of detected aggressive bouts is: 2\n",
      "1.0 \n",
      "\n",
      "video_2017-10-25T14_55_12_arena1\n",
      "Number of aggressive bouts is: 9\n",
      "Number of clean detected \"encounters\" is: 21\n",
      "Number of detected aggressive bouts is: 9\n",
      "1.0 \n",
      "\n",
      "video_2017-11-02T13_37_48_arena3\n",
      "Number of aggressive bouts is: 8\n",
      "Number of clean detected \"encounters\" is: 22\n",
      "Number of detected aggressive bouts is: 8\n",
      "1.0 \n",
      "\n",
      "video_2017-11-02T14_48_28_arena4\n",
      "Number of aggressive bouts is: 2\n",
      "Number of clean detected \"encounters\" is: 32\n",
      "Number of detected aggressive bouts is: 2\n",
      "1.0 \n",
      "\n",
      "video_2017-11-02T16_11_06_arena3\n",
      "Number of aggressive bouts is: 1\n",
      "Number of clean detected \"encounters\" is: 28\n",
      "Number of detected aggressive bouts is: 1\n",
      "1.0 \n",
      "\n",
      "video_2017-11-03T14_54_45_arena1\n",
      "No aggression for this experiment\n",
      "\n",
      "video_2017-11-03T16_20_50_arena4\n",
      "Number of aggressive bouts is: 1\n",
      "Number of clean detected \"encounters\" is: 62\n",
      "Number of detected aggressive bouts is: 1\n",
      "1.0 \n",
      "\n",
      "video_2017-12-20T12_58_51_arena4\n",
      "Number of aggressive bouts is: 2\n",
      "Number of clean detected \"encounters\" is: 34\n",
      "Number of detected aggressive bouts is: 2\n",
      "1.0 \n",
      "\n",
      "video_2017-12-20T14_01_01_arena3\n",
      "Number of aggressive bouts is: 3\n",
      "Number of clean detected \"encounters\" is: 37\n",
      "Number of detected aggressive bouts is: 3\n",
      "1.0 \n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Initialize empty data structures to store data.\n",
    "encounters_df = pd.DataFrame()\n",
    "\n",
    "flags = {condition: {} for condition in condition_order}\n",
    "clean_dict = {condition: {} for condition in condition_order}\n",
    "intersection_fractions = {condition: {} for condition in condition_order}\n",
    "\n",
    "# Start a counter to use as the index for our temporary dataframes in the loop.\n",
    "n = 0\n",
    "for condition in condition_order:\n",
    "    \n",
    "    print(condition, '\\n')\n",
    "    \n",
    "    for experiment_path in experiments[condition]:\n",
    "\n",
    "        experiment = os.path.basename(experiment_path)\n",
    "\n",
    "        # Fetch tracking data.\n",
    "        control = True if condition == 'virgin_virgin' else False\n",
    "        aggression_df, mating_df, copulation_interval, aggression_timepoints = helpers.process_track_data(experiment_path,\n",
    "                                                                                                          trackfeat_columns=['dist_to_other'],\n",
    "                                                                                                          is_control=control,\n",
    "                                                                                                          fly_id=2,\n",
    "                                                                                                          include_aggression_only=False\n",
    "                                                                                                         )\n",
    "\n",
    "        # Keep only frames within copulation time.\n",
    "        sliced_aggression = aggression_df.loc[copulation_interval[0]:copulation_interval[1]]\n",
    "\n",
    "        raw = []\n",
    "        clean = []\n",
    "\n",
    "        # Apply threshold(s).\n",
    "        proximity = sliced_aggression['dist_to_other'].values < 4\n",
    "\n",
    "        # Detect event start and end, and package them into a list of tuples.\n",
    "        if proximity[0] == True:\n",
    "            proximity = np.insert(proximity, 0, False)\n",
    "        proximity_onset = proximity[:-1] < proximity[1:]\n",
    "        proximity_offset = proximity[:-1] > proximity[1:]\n",
    "        onset_indices = np.where(proximity_onset==True)[0] + copulation_interval[0]\n",
    "        offset_indices = np.where(proximity_offset==True)[0] + copulation_interval[0]\n",
    "\n",
    "        raw_events = list(zip(onset_indices, offset_indices))\n",
    "        if len(raw_events) > 0:\n",
    "\n",
    "            # Combine events that are very close to each other into a single event.\n",
    "            clean_events = helpers.stitch_events(raw_events, suppress=True)\n",
    "\n",
    "            assert len(clean_events) <= len(raw_events), 'Stitched events list is bigger than original list: {} vs {}'.format(len(clean_events), len(raw_events))\n",
    "\n",
    "            clean_dict[condition][experiment] = clean_events\n",
    "\n",
    "            # Check for abnormally long \"encounters\", which might be indicative of tracking issues.\n",
    "            clean_events_durations = [event[1] - event[0] for event in clean_events]\n",
    "            long_durations = np.where(np.array(clean_events_durations) >= 3600)[0] # filter for bouts that are 1 minute or longer.\n",
    "            if len(long_durations) != 0:\n",
    "                flags[condition][experiment] = [clean_events[index][0] for index in long_durations]\n",
    "\n",
    "            # Get accuracy for our detector.\n",
    "            if len(aggression_timepoints) > 0:\n",
    "                nd_aggression, _, accuracy, intersect = helpers.calc_detection_accuracy(aggression_timepoints, clean_events, threshold=0.25, use_thresh=True)\n",
    "\n",
    "                intersection_fractions[condition][experiment] = intersect\n",
    "                n_bout_overlaps = len(intersect)\n",
    "                print(accuracy, '\\n')\n",
    "\n",
    "            else:\n",
    "                n_bout_overlaps = 0\n",
    "                print('No aggression for this experiment\\n')\n",
    "        \n",
    "        # In case a given experiment has no proximity encounters whatsoever.\n",
    "        else:\n",
    "            clean_dict[condition][experiment] = []\n",
    "            clean_events_durations = []\n",
    "            nd_aggression = 0\n",
    "            accuracy = 0\n",
    "            intersect = []\n",
    "            intersection_fractions[condition][experiment] = 0\n",
    "            n_bout_overlaps = 0\n",
    "            print('No encounters for this experiment\\n')\n",
    "        \n",
    "        # From the extracted data, calculate all the variables we need.\n",
    "        copulation_duration = np.diff(copulation_interval)[0]\n",
    "        aggression_total_frame_duration = sum([np.diff(timepoint)[0] for timepoint in aggression_timepoints])\n",
    "        n_encounter_frames = sum(clean_events_durations)\n",
    "        encounters_frame_ratio = n_encounter_frames / copulation_duration\n",
    "        aggression_frame_ratio = aggression_total_frame_duration / n_encounter_frames if n_encounter_frames > 0 else 0\n",
    "\n",
    "        # Create a one-row pandas DataFrame to store all the information we've calculated.\n",
    "        temp_df = pd.DataFrame(data={'condition': condition,\n",
    "                                     'experiment': experiment,\n",
    "                                     'copulation_duration': copulation_duration,\n",
    "                                     'aggression_duration': aggression_total_frame_duration,\n",
    "                                     'n_bouts_aggression': len(aggression_timepoints),\n",
    "                                     'has_aggression': True if len(aggression_timepoints) > 0 else False,\n",
    "                                     'n_encounters': len(clean_events),\n",
    "                                     'fraction_aggressive_encounters': nd_aggression / len(clean_events) if len(aggression_timepoints) > 0 else 0,\n",
    "                                     'encounters_frame_ratio': encounters_frame_ratio,\n",
    "                                     'aggression_frame_ratio': aggression_frame_ratio,\n",
    "                                     'n_bout_overlaps': n_bout_overlaps\n",
    "                                    },\n",
    "                               index=[n]\n",
    "                              )\n",
    "        \n",
    "        # Store the current experiment's data into our global DataFrame.\n",
    "        encounters_df = pd.concat([encounters_df, temp_df], axis=0)\n",
    "        \n",
    "        # Increment our counter.\n",
    "        n += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>condition</th>\n",
       "      <th>experiment</th>\n",
       "      <th>copulation_duration</th>\n",
       "      <th>aggression_duration</th>\n",
       "      <th>n_bouts_aggression</th>\n",
       "      <th>has_aggression</th>\n",
       "      <th>n_encounters</th>\n",
       "      <th>fraction_aggressive_encounters</th>\n",
       "      <th>encounters_frame_ratio</th>\n",
       "      <th>aggression_frame_ratio</th>\n",
       "      <th>n_bout_overlaps</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>virgin_virgin</td>\n",
       "      <td>video_2017-09-05T13_28_41_arena4</td>\n",
       "      <td>42083</td>\n",
       "      <td>180</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>30</td>\n",
       "      <td>0.033333</td>\n",
       "      <td>0.072904</td>\n",
       "      <td>0.058670</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>virgin_virgin</td>\n",
       "      <td>video_2017-09-05T14_34_55_arena2</td>\n",
       "      <td>54966</td>\n",
       "      <td>617</td>\n",
       "      <td>10</td>\n",
       "      <td>True</td>\n",
       "      <td>26</td>\n",
       "      <td>0.384615</td>\n",
       "      <td>0.120056</td>\n",
       "      <td>0.093499</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>virgin_virgin</td>\n",
       "      <td>video_2017-09-06T13_42_13_arena1</td>\n",
       "      <td>37771</td>\n",
       "      <td>343</td>\n",
       "      <td>6</td>\n",
       "      <td>True</td>\n",
       "      <td>31</td>\n",
       "      <td>0.193548</td>\n",
       "      <td>0.140663</td>\n",
       "      <td>0.064559</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>virgin_virgin</td>\n",
       "      <td>video_2017-09-06T14_45_32_arena3</td>\n",
       "      <td>51519</td>\n",
       "      <td>950</td>\n",
       "      <td>13</td>\n",
       "      <td>True</td>\n",
       "      <td>30</td>\n",
       "      <td>0.400000</td>\n",
       "      <td>0.159398</td>\n",
       "      <td>0.115684</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>virgin_virgin</td>\n",
       "      <td>video_2017-09-07T14_37_38_arena3</td>\n",
       "      <td>46945</td>\n",
       "      <td>579</td>\n",
       "      <td>7</td>\n",
       "      <td>True</td>\n",
       "      <td>22</td>\n",
       "      <td>0.318182</td>\n",
       "      <td>0.275642</td>\n",
       "      <td>0.044745</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       condition                        experiment  copulation_duration  \\\n",
       "0  virgin_virgin  video_2017-09-05T13_28_41_arena4                42083   \n",
       "1  virgin_virgin  video_2017-09-05T14_34_55_arena2                54966   \n",
       "2  virgin_virgin  video_2017-09-06T13_42_13_arena1                37771   \n",
       "3  virgin_virgin  video_2017-09-06T14_45_32_arena3                51519   \n",
       "4  virgin_virgin  video_2017-09-07T14_37_38_arena3                46945   \n",
       "\n",
       "   aggression_duration  n_bouts_aggression  has_aggression  n_encounters  \\\n",
       "0                  180                   1            True            30   \n",
       "1                  617                  10            True            26   \n",
       "2                  343                   6            True            31   \n",
       "3                  950                  13            True            30   \n",
       "4                  579                   7            True            22   \n",
       "\n",
       "   fraction_aggressive_encounters  encounters_frame_ratio  \\\n",
       "0                        0.033333                0.072904   \n",
       "1                        0.384615                0.120056   \n",
       "2                        0.193548                0.140663   \n",
       "3                        0.400000                0.159398   \n",
       "4                        0.318182                0.275642   \n",
       "\n",
       "   aggression_frame_ratio  n_bout_overlaps  \n",
       "0                0.058670                1  \n",
       "1                0.093499               10  \n",
       "2                0.064559                6  \n",
       "3                0.115684               12  \n",
       "4                0.044745                7  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "encounters_df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## How many \"encounters\" happen during copulation?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAHkAAAEFCAYAAADHbUhHAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+j8jraAAAgAElEQVR4nO2dd3gU1f7/X5vdZNNJJb2QBAMhkNBJQBNASkAI0gW9FK8gWPHiFfUrAj974aeCiMClCYh4FcQvSFEREAQSIECAUJOQRioh2bRt8/0DXQ0k2QTZAjOv55nnyZzZ2X1v3nvOnDlzzucjEwRBQOKexsbSAiRMj2SyCJBMFgGSySJAMlkESCaLALOYvHnzZjp16kRsbCz9+vXj0qVL6HQ6nn/+edq1a0dERARLly41hxRxIpiY6upqwdHRUbhw4YIgCIKwcOFCYciQIcKnn34qJCUlCRqNRigrKxMiIyOFw4cPm1qOKDF5TdbpdAiCwPXr1wFQqVTY29uzefNmpkyZgkKhwN3dnfHjx7Nu3TpTyxElJjfZ2dmZpUuXEh8fj7+/P4sXL+bdd98lJyeHoKAgw+sCAwPJzc1t8D3mzZuHTCYzbBItw+Qmnzp1igULFnDmzBny8/N59dVXGTVqFDqdrp5hgiAgl8sbfI958+YhCAKCNAJ7W5jc5J07d9K7d2/Cw8MBeOqpp0hPTyckJIT8/HzD6/Lz8wkMDDS1HFFicpO7dOnC3r17KSwsBGDLli20adOG5ORkVq5ciVarpby8nI0bNzJixAhTyxElClN/QL9+/XjxxRdJTEzEzs4ODw8PvvvuOyIjI7l06RIxMTGo1WqmT59OQkKCqeWIEplwl13oZDKZdG1uIdKIlwiQTBYBkskiQDJZBEgmiwDJZBEgmSwCJJNFgGSyCJBMFgGSySJAMlkESCaLAMlkESCZLAIkk0WAZLIIkEwWAZLJIkAyWQRIJosAyWQRIJksAiSTRYBksgiQT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xg9ejRqtZrw8HDWrl1rKTn3NHdd2EWJliONeIkAyWQRIJksAiSTW8DcuXOJioqiQ4cOLFy4sN6xxYsXk5iYaBlhxmgqaZTEn/zyyy9C7969BY1GI1RXVwuhoaFCRkaGIAiCcPr0aSEgIEBISEiwrMhGMGtN/uWXXxg4cCAjRowgMjLScPv0Vw4fPkxsbGy97f777zenzAZJSEhgz549KBQKioqK0Gq1ODk5UVdXx/Tp01mwYIGlJTaOOX9Re/bsEZycnIScnBxBp9MJ3bt3F7Zu3WpOCX+buXPnCo6OjsKkSZMEvV4vzJo1S1i5cqWwZ88eqSb/QVxd5GYAAAD7SURBVHR0NIGBgdjY2NC+fXvKysrqHbfWmvwH8+fPp7i4mJycHJYtW8aVK1eYMmWKpWU1idlHvOzt7Q1/N/TYsGfPnqSlpZlbllEyMjKora0lNjYWR0dHRo4cyeHDhzl9+jSxsbGoVCquXr3KuHHj+Oor60o8IvWum8nly5d54oknqKurQ61W89133zFw4EDOnj1LWloaK1asoFu3blZnMEj5k5vNkCFDOHLkCJ07d0YulzNq1CjGjx9vaVnNQhq7FgFScy0CJJNFgGSyCJBMFgGSySJAMlkESCaLAMlkESCZLAIkk0WAZLIIkEwWAZLJIkAyWQRIJouA/wPtm2pz37NtEAAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 90x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Initialize figure.\n",
    "n_conditions = encounters_df['condition'].nunique()\n",
    "figure, axis = plt.subplots(figsize=(1.25*n_conditions, 4))\n",
    "        \n",
    "# Draw the boxplot.\n",
    "sns.boxplot(x='condition',\n",
    "            y='n_encounters',\n",
    "            data=encounters_df,\n",
    "            order=condition_order,\n",
    "            palette=['steelblue'],\n",
    "            width=0.6,\n",
    "            showfliers=False,\n",
    "            boxprops={'edgecolor': INK},\n",
    "            capprops={'color': INK},\n",
    "            medianprops={'color': INK},\n",
    "            whiskerprops={'color': INK}\n",
    "           )\n",
    "\n",
    "# Draw the swarmplot.\n",
    "sns.swarmplot(x='condition',\n",
    "              y='n_encounters',\n",
    "              data=encounters_df,\n",
    "              order=condition_order,\n",
    "              palette=['steelblue'],\n",
    "              linewidth=0.75,\n",
    "              edgecolor=INK\n",
    "             )\n",
    "\n",
    "# Figure and axes formatting.\n",
    "axis.set_xlabel('')\n",
    "axis.set_xticklabels([''])\n",
    "axis.set_ylabel('# Encounters')\n",
    "axis.set_ylim(0, 80)\n",
    "axis.set_yticks(range(0, 80+1, 20))\n",
    "axis.set_yticklabels(list(range(0, 80+1, 20)))\n",
    "\n",
    "# Table definition.\n",
    "row1 = [str(encounters_df.query('condition==\"'+condition+'\"').shape[0]) for condition in condition_order]\n",
    "cell_text = np.array([row1])\n",
    "\n",
    "row_labels = ['n =']\n",
    "summary_table = axis.table(cellText= cell_text,\n",
    "                           cellLoc='center',\n",
    "                           rowLabels=row_labels,\n",
    "                           rowLoc='center',\n",
    "                           colLoc='center'\n",
    "                          )\n",
    "\n",
    "summary_table.auto_set_font_size(False)\n",
    "summary_table.set_fontsize(11)\n",
    "\n",
    "properties = summary_table.properties()\n",
    "table_cells = properties['children']\n",
    "for cell in table_cells:\n",
    "    cell.set_height(0.12)\n",
    "    cell.set_alpha(0)\n",
    "\n",
    "# Draw statistical results.\n",
    "for p, condition in enumerate(condition_order[1:]):\n",
    "    sig_height = 6 if (corrected_encounters_pvalues.get(condition, 'NaN') == 'NaN') or (corrected_encounters_pvalues.get(condition, 'NaN') >= 0.05) else 2\n",
    "    helpers.plot_stattest_result(ax=axis,\n",
    "                                 x1=p+1,\n",
    "                                 x2=p+1,\n",
    "                                 p_value=corrected_encounters_pvalues.get(condition, 'NaN'),\n",
    "                                 y=97,\n",
    "                                 ticksize=0,\n",
    "                                 xytext=(0,sig_height),\n",
    "                                 fontsize=13,\n",
    "                                 color=INK,\n",
    "                                 connector_color=INK\n",
    "                                )\n",
    "\n",
    "# Saving parameters.\n",
    "filename = 'n_encounters_during_copulation'\n",
    "plt.savefig(os.path.join(savepath, filename))\n",
    "\n",
    "plt.show()\n",
    "plt.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
